//
//  ItemView.m
//  ZYShang
//
//  Created by wangguangming on 15/8/4.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "ItemView.h"
#import "Base.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
#import "UIImage+imagedown.h"
@implementation ItemView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
        {
        self.backgroundColor=[UIColor whiteColor];
        [self addall];
        }
    return self;
}
-(void)addall
{  
    
    self.itemImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH/5, WIDTH/5)];
    self.itemImage.contentMode=UIViewContentModeScaleAspectFit;
    [self addSubview:self.itemImage];
    
    
    self.titleLaber=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.itemImage.frame), 0, WIDTH*3/10, WIDTH/10)];
    self.titleLaber.textColor=[UIColor grayColor];
    [self.titleLaber setAdjustsFontSizeToFitWidth:YES];
    self.pieceLaber=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.itemImage.frame), CGRectGetMaxY(self.titleLaber.frame), WIDTH*3/10, WIDTH/10)];
    [self.pieceLaber setAdjustsFontSizeToFitWidth:YES];
    self.pieceLaber.textColor=[UIColor grayColor];
    [self addSubview:self.pieceLaber];
    [self addSubview:self.titleLaber];
    
    self.suButton=[[UIButton alloc] initWithFrame:CGRectMake(WIDTH/2+5, 5, (WIDTH/2-10)/3, (WIDTH/2-10)/3)];
    [self.suButton setTitle:@"减" forState:UIControlStateNormal];
    
    [self.suButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.suButton.layer.borderColor=[[UIColor grayColor] CGColor];
    self.suButton.layer.borderWidth=1.0;
    
    self.numberLaber=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.suButton.frame), 5, (WIDTH/2-10)/3, (WIDTH/2-10)/3)];
    self.numberLaber.text=@"0";
    self.numberLaber.textColor=[UIColor grayColor];
    self.numberLaber.textAlignment=NSTextAlignmentCenter;
    self.suButton.layer.borderColor=[COLOR CGColor];
    self.suButton.layer.borderWidth=1.0;
    self.suButton.layer.cornerRadius=(WIDTH/2-10)/6;
    
    self.addButton=[[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.numberLaber.frame), 5, (WIDTH/2-10)/3, (WIDTH/2-10)/3)];
   [self.addButton setTitle:@"加" forState:UIControlStateNormal];
    self.addButton.layer.borderColor=[COLOR CGColor];
     [self.addButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.addButton.layer.borderWidth=1.0;
    self.addButton.layer.cornerRadius=(WIDTH/2-10)/6;
    
    
    [self.addButton addTarget:self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
     [self.suButton addTarget:self action:@selector(suAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.suButton];
    [self addSubview:self.numberLaber];
    [self addSubview:self.addButton];
    
    
}
-(void)addAction:(UIButton *)sender
{
    int a=self.numberLaber.text.integerValue;
    a++;
     self.numberLaber.text=[NSString stringWithFormat:@"%d",a];
}
-(void)suAction:(UIButton *)sender
{
    int a=self.numberLaber.text.integerValue;
    if (a>0)
        {
        a--;
      }
    else
        {
        a=0;
        }
    self.numberLaber.text=[NSString stringWithFormat:@"%d",a];
}
-(void)setImage:(Items *)items
{
    self.itemImage.image=[UIImage imageDownWithurlstring:items.pic imagebolock:^(UIImage *img) {
        self.itemImage.image=img;
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
